var typedatas = [{id:'1',value:'1',text:"幼儿园"},
                  {id:'2',value:'2',text:"小学"},
                  {id:'3',value:'3',text:"初中"},
                  {id:'4',value:'4',text:"高中"},
                  {id:'5',value:'5',text:"中职"}
                  ];
$(function(){
	var searchform = $("#formsearch");
	var fields = [
	              {display:"操作人姓名",name:"optid",newline:false,labelWidth:100,width:220,space:30,type:"text",cssClass:"field",validate : {required : true,maxlength : 25}},
	              {display:"学校名称",name:"orgid",newline:false,labelWidth:100,width:220,space:30,type:"text",cssClass:"field",validate : {required : true,maxlength : 50}},
				  {display:"学校办学类型",name:"type",newline:false,labelWidth:100,width:220,space:30, type: "select",validate : {required : true},
						comboboxName: "filter_type", attr: { op: "equal" }, cssClass: "field",options: { valueFieldID: "type", valueField:"id",data:typedatas}}
				 ];
	$("#formsearch").ligerForm({fields : fields});
//	var container = $('<ul><li style="margin-right:8px"></li></ul><div class="l-clear"></div>').appendTo(searchform);
//	LG.createButton({
//		appendTo: container.find("li:eq(0)"),
//		text: "使用说明书下载",
//		width:"160",
//		click: function (){
//		}
//	});
	//创建按钮 学校
	LG.createButton({
		appendTo: $("#imp_info_ul_school").find("li:eq(1)"),
		text: "导入",
		click: function (){
			jQuery.metadata.setType("attr", "validate"); 
			LG.validate(searchform, { debug: true });
			if(searchform.valid()){
				var type = liger.get("filter_type").getValue();
				batchimport_school(type);
			}
		},
		style: {width:'70px'}
	});
	//创建按钮 学校
	LG.createButton({
		appendTo: $("#imp_info_ul_school").find("li:eq(2)"),
		text: "导入模板下载",
		click: function (){
			jQuery.metadata.setType("attr", "validate"); 
			LG.validate(searchform, { debug: true });
			if(searchform.valid()){
				var type = liger.get("filter_type").getValue();
				if(type == '1'){
					location.href=basepath + "pages/importInfo/schoolNurseryEdu_import.xls";
				}else if(type == '5'){
					location.href=basepath + "pages/importInfo/schoolVocationlEdu_import.xls";
				}else{
					location.href=basepath + "pages/importInfo/schoolBasicEdu_import.xls";
				}
			}
		},
		style: {width:'90px'}
	});
	//创建按钮 学校
	LG.createButton({
		appendTo: $("#imp_info_ul_school").find("li:eq(3)"),
		text: "问卷下载",
		click: function (){
			jQuery.metadata.setType("attr", "validate"); 
			LG.validate(searchform, { debug: true });
			if(searchform.valid()){
				var type = liger.get("filter_type").getValue();
				if(type == '5'){
					location.href=basepath + "pages/importInfo/doc/t_xuexiao_zhongzhi.doc";
				}else{
					location.href=basepath + "pages/importInfo/doc/t_xuexiao_jichujiaoyu.doc";
				}
			}
		},
		style: {width:'90px'}
	});
/*	//创建按钮 老师
	LG.createButton({
		appendTo: $("#imp_info_ul_teacher").find("li:eq(1)"),
		text: "导入",
		click: function (){
			jQuery.metadata.setType("attr", "validate"); 
			LG.validate(searchform, { debug: true });
			if(searchform.valid()){
				var type = liger.get("filter_type").getValue();
				batchimport_teacher(type);
			}
		},
		style: {width:'70px'}
	});
	//创建按钮 老师
	LG.createButton({
		appendTo: $("#imp_info_ul_teacher").find("li:eq(2)"),
		text: "导入模板下载",
		click: function (){
			jQuery.metadata.setType("attr", "validate"); 
			LG.validate(searchform, { debug: true });
			if(searchform.valid()){
				var type = liger.get("filter_type").getValue();
				if(type == '5'){
					//中职
					location.href=basepath + "pages/importInfo/importExcelTemplet_j1.xls";
				} else{
					//基础
					location.href=basepath + "pages/importInfo/importExcelTemplet_j3.xls";
				}
			}
		},
		style: {width:'90px'}
	});
	//创建按钮 老师
	LG.createButton({
		appendTo: $("#imp_info_ul_teacher").find("li:eq(3)"),
		text: "问卷下载",
		click: function (){
			jQuery.metadata.setType("attr", "validate"); 
			LG.validate(searchform, { debug: true });
			if(searchform.valid()){
				var type = liger.get("filter_type").getValue();
				if(type == '5'){
					location.href=basepath + "pages/importInfo/doc/t_jiaoshi_zhongzhi.doc";
				}else{
					location.href=basepath + "pages/importInfo/doc/t_jiaoshi_jichujiaoyu.doc";
				}
			}
		},
		style: {width:'90px'}
	});*/
	//创建按钮 学生
	LG.createButton({
		appendTo: $("#imp_info_ul_student").find("li:eq(1)"),
		text: "导入",
		click: function (){
			jQuery.metadata.setType("attr", "validate"); 
			LG.validate(searchform, { debug: true });
			if(searchform.valid()){
				var type = liger.get("filter_type").getValue();
				batchimport_student(type);
			}
		},
		style: {width:'70px'}
	});
	//创建按钮 学生
	LG.createButton({
		appendTo: $("#imp_info_ul_student").find("li:eq(2)"),
		text: "导入模板下载",
		click: function (){
			jQuery.metadata.setType("attr", "validate"); 
			LG.validate(searchform, { debug: true });
			if(searchform.valid()){
				var type = liger.get("filter_type").getValue();
				if(type == '1'){
					//幼儿园
					location.href=basepath + "pages/importInfo/importTemplate_cheeper.xls";
				} else if(type == '2') {
					//小学
					location.href=basepath + "pages/importInfo/importTemplate_primary.xls";
				} else if(type == '3') {
					//初中
					location.href=basepath + "pages/importInfo/importTemplate_middle.xls";
				} else if(type == '4') {
					//高中
					location.href=basepath + "pages/importInfo/importTemplate_senior.xls";
				} else {
					//中职
					location.href=basepath + "pages/importInfo/importExcelTemplet_j2.xls";
				}
			}
		},
		style: {width:'90px'}
	});
	//创建按钮 学生
	LG.createButton({
		appendTo: $("#imp_info_ul_student").find("li:eq(3)"),
		text: "问卷下载",
		click: function (){
			jQuery.metadata.setType("attr", "validate"); 
			LG.validate(searchform, { debug: true });
			if(searchform.valid()){
				var type = liger.get("filter_type").getValue();
				if(type == '1'){
					//幼儿园
					location.href=basepath + "pages/importInfo/doc/t_wenjuan_youeryuanxuesheng.doc";
				} else if(type == '2') {
					//小学
					location.href=basepath + "pages/importInfo/doc/t_wenjuan_xiaoxuexuesheng.doc";
				} else if(type == '3') {
					//初中
					location.href=basepath + "pages/importInfo/doc/t_wenjuan_chuzhongxuesheng.doc";
				} else if(type == '4') {
					//高中
					location.href=basepath + "pages/importInfo/doc/t_wenjuan_gaozhongxuesheng.doc";
				} else {
					//中职
					location.href=basepath + "pages/importInfo/doc/t_wenjuan_zhongzhixuesheng.doc";
				}
			}
		},
		style: {width:'90px'}
	});
	
	function f_cancel(){
        var win = parent || window;
        win.LG.closeCurrentTab(null);
    }
	
	function f_save(){
        var win = parent || window;
        win.LG.closeCurrentTab(null);
    }
});

function batchimport_school(type) {
	var imgurl = $("#uploadfile_school").val();
	var extend = imgurl.substring(imgurl.lastIndexOf("."), imgurl.length);
	extend = extend.toLowerCase();
	if (extend != ".xls") {
		LG.showError("请上传后缀为xls的excel文件");
		return;
	}
	var url;
	if(type == '1'){
		//学前
		url = basepath + "nursery_school_import/doimport.do?optid="+encodeURI(encodeURI($("#optid").val()))+"&&orgid="+encodeURI(encodeURI($("#orgid").val()));
	}else if(type == '5'){
		//中职
		url = basepath + "schoolVocation/doimport.do?optid="+encodeURI(encodeURI($("#optid").val()))+"&&orgid="+encodeURI(encodeURI($("#orgid").val()));
	}else {
		//基础
		url = basepath + "schoolBasicEdu/doimport.do?optid="+encodeURI(encodeURI($("#optid").val()))+"&&orgid="+encodeURI(encodeURI($("#orgid").val()));
	}
	LG.showLoading("导入中...请等待");
	$.ajaxFileUpload({
		url : url, // --上传-
		secureuri : false,
		fileElementId : "uploadfile_school",
		dataType : "json",
		success : function(data, status) {
			if(data['IsError']){
				LG.showError(data['Message']);
			}else{
				LG.showSuccess(data['Message']);
			}
			$("#textfield_school").val('');
			LG.hideLoading();
		}
	});
}

function batchimport_teacher(type) {
	var imgurl = $("#uploadfile_teacher").val();
	var extend = imgurl.substring(imgurl.lastIndexOf("."), imgurl.length);
	extend = extend.toLowerCase();
	if (extend != ".xls") {
		LG.showError("请上传后缀为xls的excel文件");
		return;
	}
	var url;
	if(type == '5'){
		//中职
		url = basepath + "teacherVocation/doimport.do?optid="+encodeURI(encodeURI($("#optid").val()))+"&&orgid="+encodeURI(encodeURI($("#orgid").val()));
	} else {
		//基础
		url = basepath + "teacherBasicEdu/doimport.do?optid="+encodeURI(encodeURI($("#optid").val()))+"&&orgid="+encodeURI(encodeURI($("#orgid").val()));
	}
	LG.showLoading("导入中...请等待");
	$.ajaxFileUpload({
		url : url, // --上传-
		secureuri : false,
		fileElementId : "uploadfile_teacher",
		dataType : "json",
		data: {
            optid: $("#optid").val()
        },
		success : function(data, status) {
			if(data['IsError']){
				LG.showError(data['Message']);
			}else{
				LG.showSuccess(data['Message']);
			}
			$("#textfield_teacher").val('');
			LG.hideLoading();
		}
	});
}

function batchimport_student(type) {
	var imgurl = $("#uploadfile_student").val();
	var extend = imgurl.substring(imgurl.lastIndexOf("."), imgurl.length);
	extend = extend.toLowerCase();
	if (extend != ".xls") {
		LG.showError("请上传后缀为xls的excel文件");
		return;
	}
	var url;
	if(type == '1'){
		//幼儿园
		url = basepath + "studentCheeper/doimport.do?optid="+encodeURI(encodeURI($("#optid").val()))+"&&orgid="+encodeURI(encodeURI($("#orgid").val()));
	} else if(type == '2') {
		//小学
		url = basepath + "studentPrimary/doimport.do?optid="+encodeURI(encodeURI($("#optid").val()))+"&&orgid="+encodeURI(encodeURI($("#orgid").val()));
	} else if(type == '3') {
		//初中
		url = basepath + "studentMiddle/doimport.do?optid="+encodeURI(encodeURI($("#optid").val()))+"&&orgid="+encodeURI(encodeURI($("#orgid").val()));
	} else if(type == '4') {
		//高中
		url = basepath + "studentSenior/doimport.do?optid="+encodeURI(encodeURI($("#optid").val()))+"&&orgid="+encodeURI(encodeURI($("#orgid").val()));
	} else {
		//中职
		url = basepath + "studentVocation/doimport.do?optid="+encodeURI(encodeURI($("#optid").val()))+"&&orgid="+encodeURI(encodeURI($("#orgid").val()));
	}
	LG.showLoading("导入中...请等待");
	$.ajaxFileUpload({
		url : url, // --上传-
		secureuri : false,
		fileElementId : "uploadfile_student",
		dataType : "json",
		data: {
            optid: $("#optid").val()
        },
		success : function(data, status) {
			if(data['IsError']){
				LG.showError(data['Message']);
			}else{
				LG.showSuccess(data['Message']);
			}
			$("#textfield_student").val('');
			LG.hideLoading();
		}
	});
}